<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class Blocks extends Base{
    public function block_list(){
        $keyword=input('get.keyword');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $list=Db::name('block')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('guid|city|street|plate|estate|block','like',"%{$keyword}%")->where('state','<',3)->order('id desc')->paginate(10);
        $count=Db::name('block')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('guid|city|street|plate|estate|block','like',"%{$keyword}%")->where('state','<',3)->count();
        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('count',$count);
        return $this->fetch();
    }
    public function block_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['guid']=$param['guid'];
            $data['city']=$param['city'];
            $data['street']=$param['street'];
            $data['plate']=$param['plate'];
            $data['estate']=$param['estate'];
            $data['block']=$param['block'];
            $data['qrcode']=$param['qrcode'];
            $data['img']=$param['img'];
            $data['time']=time();
            $data['uptime']=time();
            $res= Db::name('block')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function block_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('block')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['guid']=$param['guid'];
            $data['city']=$param['city'];
            $data['street']=$param['street'];
            $data['plate']=$param['plate'];
            $data['estate']=$param['estate'];
            $data['block']=$param['block'];
            $data['qrcode']=$param['qrcode'];
            $data['img']=$param['img'];
            $data['uptime']=time();
            $res=Db::name('block')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
        }
    }
    public function block_del(){
        $param = input('post.');
        $res= Db::name('block')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }

    // 生成二维码
    public function qrcodes()
    {   
        $all=Db::name('block')->field('id,qrcode')->where('state',1)->order('id asc')->select();
        foreach ($all as $key => $value) {
            // type(1:街道巷;2:楼幢;3:单元;4:室)
            $url=config('http_https').$_SERVER['HTTP_HOST'].'/index/index/index.html?type=2&id='.$all[$key]['id'];
            $qrcode_path=$this->scerweima($url,2,$all[$key]['id']);
            Db::name('block')->where('id',$all[$key]['id'])->update(['qrcode'=>$qrcode_path]);
            $date_path=substr($all[$key]['qrcode'],16,8);
            if ($all[$key]['qrcode']&&file_exists("../public".$all[$key]['qrcode'])&&date('Ymd',time())>$date_path) {
                unlink("../public".$all[$key]['qrcode']);
            }
        }
        return json(['code' => 200,'msg' => '成功']);
    }
    // 生成牌
    public function plates()
    {
        $all=Db::name('block')->field('id,street,estate,qrcode,img,block,plate')->where('state',1)->order('id asc')->select();
        foreach ($all as $key => $value) {
            // type(1:街道巷;2:楼幢;3:单元;4:室)
            if ($all[$key]['estate']) {
                $char=$all[$key]['estate'];
                $pinyin=$this->get_all_py($all[$key]['estate']);
            }else{
                $char=$all[$key]['street'];
                $pinyin=$this->get_all_py($all[$key]['street']);
            }
            $pinyin=implode(' ',$pinyin);
            if ($all[$key]['block']) {
                $number=$all[$key]['block'];
            }else{
                $number=$all[$key]['plate'];
            }
            $plate_path=$this->plateImg($all[$key]['id'],$all[$key]['qrcode'],2,$char,$pinyin,$number);
            Db::name('block')->where('id',$all[$key]['id'])->update(['img'=>$plate_path]);
            $date_path=substr($all[$key]['img'],16,8);
            if ($all[$key]['img']&&file_exists("../public".$all[$key]['img'])&&date('Ymd',time())>$date_path) {
                unlink("../public".$all[$key]['img']);
            }
        }
        return json(['code' => 200,'msg' => '成功']);
    }
}